//
//  STDSVignetteEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/4/3.
//

#ifndef STDSVignetteEffect_h
#define STDSVignetteEffect_h

#include "STDSBaseEffect.hpp"

class STDSVignetteEffect: public STDSBaseEffect {
public:
    STDSVignetteEffect() {
        mEffectType = STDSEffectTypeVignette;
    }
    ~STDSVignetteEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
        if (strcmp("strength", name) == 0) {
            mStrength = *floatValue;
        } else if (strcmp("blur", name) == 0) {
            mBlur = *floatValue;
        }
    }
    
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mVignetteProgram == nullptr) {
            mVignetteProgram = mContext->getProgram(kShaderSRCVignette);
        }
        return mVignetteProgram;
    }
    
    void draw() override {
        auto program = getEffectProgram();
        if (program == nullptr) {
            return;
        }
        if (mTexture == nullptr) {
            return;
        }
        
        program->use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
        program->setUniform(program->getUniform("inputImageTexture"), 0);
        program->setUniform(program->getUniform("strength"), mStrength);
        program->setUniform(program->getUniform("blur"), mBlur);
        
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    float mStrength{0.0};
    float mBlur{0.0};
    shared_ptr<STDSShaderProgram> mVignetteProgram{nullptr};
};

#endif /* STDSVignetteEffect_h */
